package com.ideaaedi.zoo.commonbase.util;


import com.ideaaedi.zoo.commonbase.context.DefaultZcProvider;
import com.ideaaedi.zoo.commonbase.context.ZcProvider;
import lombok.extern.slf4j.Slf4j;

import javax.annotation.Nonnull;
import java.util.Map;

/**
 * 上下文工具
 *
 * @author <font size = "20" color = "#3CAA3C"><a href="https://gitee.com/JustryDeng">JustryDeng</a></font> <img
 * src="https://gitee.com/JustryDeng/shared-files/raw/master/JustryDeng/avatar.jpg" />
 * @since 1.0.0
 */
@Slf4j
public final class ZooContext {
    
    private static ZcProvider delegate = new DefaultZcProvider();
    
    /**
     * 操作上下文的内部能力
     */
    private static ZcProvider.Inner Inner = delegate.provideInner();
    
    /**
     * 认证鉴权
     */
    public static ZcProvider.Auth Auth = delegate.provideAuth(Inner);
    
    /**
     * http请求响应
     */
    public static ZcProvider.Http Http = delegate.provideHttp();
    
    /**
     * 租户数据域
     */
    public static ZcProvider.TenantCt TenantCt = delegate.provideTenantCt(Inner);
    
    /**
     * 审计日志
     */
    public static ZcProvider.AuditLog AuditLog = delegate.provideAuditLog(Inner);
    
    /**
     * 复合操作
     */
    public static ZcProvider.Complex Complex = delegate.provideComplex(Auth, TenantCt);
    
    @Nonnull
    public static Map<String, Object> unmodifiableContext() {
        return delegate.unmodifiableContext();
    }
    
    public static void clear() {
        delegate.clear();
    }
    
    /**
     * 设置ZooContext代理
     * <pre>
     * 注：慎用此方法，如果要用的话，最好在任何调用ZooContext方法的逻辑执行之前调用
     * </pre>
     */
    public synchronized static void setDelegate(ZcProvider delegate) {
        ZooContext.delegate = delegate;
        Inner = delegate.provideInner();
        Http = delegate.provideHttp();
        Auth = delegate.provideAuth(Inner);
        TenantCt = delegate.provideTenantCt(Inner);
        AuditLog = delegate.provideAuditLog(Inner);
        Complex = delegate.provideComplex(Auth, TenantCt);
    }
    
}